package com.hspedu.list_;

import java.util.*;

/**
 * List实现类的课堂练习<br>
 * 使用List的实现类添加三本图书, 并遍历, 打印出如下效果<br>
 * 名称: xx   价格: xx  作者: xx<br>
 * 名称: xx   价格: xx  作者: xx<br>
 * 名称: xx   价格: xx  作者: xx<br>
 * <p>
 * 要求:<br>
 * 1.按照价格排序, 从低到高(使用冒泡排序)<br>
 * 2.要求使用ArrayList LinkedList Vector三种集合实现<br>
 *
 * @author enomix
 * @date 2022-06-16 14:24
 */
public class ListExercise02 {
    public static void main(String[] args) {

        //List list = new ArrayList();
        //List list = new LinkedList();
        List list = new Vector();
        list.add(new Book("红楼梦", 100, "曹雪芹"));
        list.add(new Book("西游记", 10, "吴承恩"));
        list.add(new Book("水浒传", 9, "施耐庵"));
        list.add(new Book("三国", 80, "罗贯中"));

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);

        }



        //1.重写接口排序
        //comparatorSort(list);

        //2.冒泡排序
        sort(list);

        System.out.println();
        iterator = list.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);

        }
    }

    //静态的冒泡排序方法
    public static void sort(List list){

        int listSize = list.size();
        for (int i = 0; i < listSize; i++){
            for (int j = 0; j < listSize - 1 - i; j++) {
                Book book1 = (Book) list.get(j);
                Book book2 = (Book) list.get(j+1);
                if (book1.getPrice() > book2.getPrice()){
                    list.set(j, book2);
                    list.set(j + 1, book1);
                }
            }
        }
    }

    //静态的重写Comparator接口方法
    public static void comparatorSort(List list){
        //1.重写Comparator接口
        list.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                double count = b1.getPrice() - b2.getPrice();
                if (count > 0) {
                    return 1;
                } else if (count < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
    }
}

class Book {
    private String name;
    private double price;
    private String author;

    public Book() {
    }

    public Book(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "名称: " + name + '\t' +
                "价格: " + price + '\t' +
                "作者: " + author + '\t';
    }
}